package com.milkshopbackend.milkshop.Service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.milkshopbackend.milkshop.Service.AddressService;
import com.milkshopbackend.milkshop.entity.Address;
import com.milkshopbackend.milkshop.mapper.AddressMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
    @Override
    public List<Address> getUserAddresses(Long userId) {
        return lambdaQuery().eq(Address::getUserId, userId).list();
    }

    @Override
    @Transactional
    public void setDefaultAddress(Long userId, Long addressId) {
        // 先将该用户所有地址的isDefault设为0
        lambdaUpdate().eq(Address::getUserId, userId).set(Address::getIsDefault, 0).update();
        // 再将指定地址设为1
        lambdaUpdate().eq(Address::getId, addressId).eq(Address::getUserId, userId).set(Address::getIsDefault, 1).update();
    }
} 